###########################################################

library(dplyr)
library(tidyr)
library(nnet)
library(data.table)
# Make all the tables 
# Read in fles
setwd("Derived Data")
library(haven)
# Bartik:   ############### IV
dfbartik=fread("dfbartik2005_40kmanalysis_annual.csv")

tempvarsavg="ay_mean_snow  +aymean_high_num_snow+aymean_severe_num_snow+ay_avg_mean_max + ay_avg_mean_min  + ay_avg_bint80  + ay_avg_bint90  + ay_avg_bint100  + ay_avg_bint20 + ay_avg_bint10+ ay_avg_bint0+ ay_mean_prcp +aymean_high_num_prcp+aymean_severe_num_prcp"

districtcovariates="pct_hisp_2005:factor(year) + pct_black_2005:factor(year) + pct_male_2005:factor(year) + pct_white_2005:factor(year) + enroll_2005:factor(year) + pct_ell_2005:factor(year) + pct_speced_2005:factor(year) + pct_FRPM_2005:factor(year) + missing_pct_ell_2005 + missing_pct_speced_2005 + missing_pct_FRPM_2005 "
sorting="moveinrate_2000:factor(year) + moveoutrate_2000:factor(year)"

acs="pctemploy2005:factor(year)  + pctlaborforce2005:factor(year) + pctsinglemother2005:factor(year) + pctmanufac2005:factor(year) + pctutility2005:factor(year) + pctbachelorhigher2005:factor(year)"

############### Bartik 2005 IV ################# 

# get average score across grades
dfbartik_may=dfbartik %>% filter(testmonth=="May" & subject=="rla") %>% group_by(leaid,year) %>% mutate(avgscore=mean(cs_mn_all),avgscore_lag=mean(cs_mn_all_lag_cohort))

dfbartik_may=dfbartik_may[dfbartik_may$grade==4,]
dfbartik_may=dfbartik_may %>% drop_na(avgscore,avgpm25_9, coalshare_40,gasshare_40,renewableshare_40,oilshare_40, avgscore_lag, malpct_test,pct_white_2005,pct_black_2005,pct_hisp_2005,pct_FRPM_2005,pct_speced_2005,pct_ell_2005,pctbachelorhigher2005,pctemploy2005,pctsinglemother2005)


##########################################
# Rotemberg weights
library(bartik.weight)

# Master df:
dfmaster=dfbartik_may[,c("totalprod_fuel40","leaid","year","grade","subject","cs_mn_all","avgpm25_9","pct_white_2005","pct_black_2005","pct_hisp_2005","pct_FRPM_2005","pct_speced_2005","pct_ell_2005","pctbachelorhigher2005","pctemploy2005","pctsinglemother2005")]


dfmasterwide_test=with(dfbartik_may,data.frame(class.ind(year),class.ind(grade), avgscore,totalprod_fuel40,avgpm25_9,pct_white_2005,pct_black_2005,pct_hisp_2005,pct_FRPM_2005,pct_speced_2005,pct_ell_2005,pctbachelorhigher2005,pctemploy2005,pctsinglemother2005))

############### create year for the controls

for(year in 2011:2018){
  colyear=paste("X",year,sep="")
  
  for(demog in c("pct_white_2005","pct_black_2005","pct_hisp_2005","pct_FRPM_2005","pct_speced_2005","pct_ell_2005","pctbachelorhigher2005","pctemploy2005","pctsinglemother2005")){
    
    demog_year=paste("t",year,"_",demog,sep="")
    
    dfmasterwide_test[[demog_year]]=dfmasterwide_test[[colyear]]*dfmasterwide_test[[demog]]
    
  }
  
}


# global df:
df_growth_wide_test=unique(dfbartik_may[,c("growthcoal","growthoil","growthrenewable","growthgas","year")])
df_growth_test <- gather(df_growth_wide_test, fuelold, growth, growthcoal:growthgas, factor_key=FALSE)
df_growth_test$fuel[df_growth_test$fuelold=="growthcoal"]="coal"; df_growth_test$fuel[df_growth_test$fuelold=="growthoil"]="oil"; df_growth_test$fuel[df_growth_test$fuelold=="growthgas"]="gas"; df_growth_test$fuel[df_growth_test$fuelold=="growthrenewable"]="renew"
df_growth_test$fuelold=NULL

# local df:
df_local_test=unique(dfbartik_may[,c("coalshare_40","oilshare_40","gasshare_40","renewableshare_40","leaid","year")])
df_localwide_test=with(df_local_test, data.frame(class.ind(year), leaid,coalshare_40,oilshare_40,gasshare_40,renewableshare_40))
# localdf: year by industry share
for(year in 2010:2018){
  colyear=paste("X",year,sep="")
  
  for(fuel in c("coal","gas","renewable","oil")){
    
    fuel_year=paste("t",year,"_init_sh_",fuel,sep="")
    
    share=paste(fuel,"share_40",sep="")
    df_localwide_test[[fuel_year]]=df_localwide_test[[colyear]]*df_localwide_test[[share]]
    
  }
  
}

index=c("leaid","year")
y="avgscore"
x="avgpm25_9"
weight="totalprod_fuel40"
controls = setdiff(names(dfmasterwide_test), c(index, y, x, weight,"pct_white_2005","pct_black_2005","pct_hisp_2005","pct_FRPM_2005","pct_speced_2005","pct_ell_2005","pctbachelorhigher2005","pctemploy2005","pctsinglemother2005"))
#controls=c("rla", "mth")
Z=names(df_localwide_test[15:50])
G="growth"


# across grades: df_growth_test is the same and dflocalwide 
df_localwide_test=df_localwide_test %>% arrange(X2010,X2011,X2012,X2013,X2014,X2015,X2016,X2017,X2018)
dfmasterwide_test=dfmasterwide_test %>% arrange(X2010,X2011,X2012,X2013,X2014,X2015,X2016,X2017,X2018)
df_growth_test=df_growth_test %>% arrange(year,fuel)

bw=bw(dfmasterwide_test,y,x,NULL,weight,df_localwide_test,Z,df_growth_test,G)
dfout=bw %>% group_by(fuel) %>% summarize(alpha_sum=sum(alpha))
write.csv(dfout,"../output/rotemberg.csv")
